/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.model.newmodel;

import com.cxhd.lis.util.DateUtil;
import com.cxhd.lis.util.ToolDateTime;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import java.sql.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author cxhd
 */
@SuppressWarnings("serial")
public class lisTestResult extends Model<lisTestResult> {

    public static final lisTestResult lisUserdao = new lisTestResult();

    public void deleteResult(String UUID) throws Exception {
        Db.update("delete from lis_testresult where UUID='" + UUID + "'");
//        Db.use("sqlserver").update("delete MS_YJ01 where SQID="+sqdh);
//        findById(pk).delete();
    }

    public void deletePK(String pk) throws Exception {
        deleteById(pk);
    }

    public void addResult(Map<String, String> map, String ybh, String UUID, String datetime, String deviceId) throws Exception {
//        String pk = map.get("PK");
//        if (map.get("PK") == null) {
//            pk = getPk();
//        }
//        System.out.println(pk);
        if (!StringUtils.isBlank(map.get("TESTID"))) {
            set("PK", null)
                    .set("SAMPLENO", ybh)
                    .set("TESTID", map.get("TESTID"))
                    .set("ITEMPK", map.get("ITEMPK"))
                    .set("DEVICEID", map.get("DEVICEID") == null ? deviceId : map.get("DEVICEID"))
                    .set("UUID", UUID)
                    .set("RESULTFLAG", map.get("RESULTFLAG"))
                    .set("TESTRESULT", map.get("TESTRESULT"))
                    .set("TESTSTATUS", map.get("TESTSTATUS"))
                    .set("SAMPLETYPE", map.get("SAMPLETYPE"))
                    .set("FEE", map.get("FEE"))
                    //                .set("DEVICEID", map.get("DEVICEID"))
                    .set("MEASURETIME", map.get("MEASURETIME") == null ? datetime : map.get("MEASURETIME"))
                    .set("UNIT", map.get("UNIT"))
                    .save();
        }

    }
    
    public void addResult(Map<String, String> map, String ybh, String UUID, String datetime) throws Exception {
     
       
        if (!StringUtils.isBlank(map.get("TESTID"))) {
                    set("PK", null)
                    .set("SAMPLENO", ybh)
                    .set("TESTID", map.get("TESTID"))
                    .set("ITEMPK", map.get("ITEMPK"))
                    .set("DEVICEID", "MB")
                    .set("UUID", UUID)
                    .set("RESULTFLAG", map.get("RESULTFLAG"))
                    .set("TESTRESULT", map.get("TESTRESULT"))
                    .set("TESTSTATUS", map.get("TESTSTATUS"))
                    .set("SAMPLETYPE", map.get("SAMPLETYPE"))
                    .set("FEE", map.get("FEE"))
                    //                .set("DEVICEID", map.get("DEVICEID"))
                    .set("MEASURETIME", map.get("MEASURETIME") == null ? datetime : map.get("MEASURETIME"))
                    .set("UNIT", map.get("UNIT"))
                    .save();
        }

    }

    public String getPk() {
        String value = (Integer.parseInt(Db.findFirst("SELECT MAX(pk) as id from lis_testresult").get("id") + "") + 1) + "";
        return value;
    }

    public String getSampleNO(String value) {

        Record re = Db.findFirst("select MAX(SAMPLENO) as SAMPLENO FROM lis_testresult WHERE date(MEASURETIME)=CURDATE() and SAMPLENO LIKE '" + value + "%'");
        if (re == null || re.get("SAMPLENO") == null) {

            return value + "0001";

        }
        String sample = (Integer.parseInt(re.getStr("SAMPLENO").replace(value, "")) + 1) + "";
        System.out.println(sample);
        if (sample.length() == 1) {
            sample = "000" + sample;
        } else if (sample.length() == 2) {
            sample = "00" + sample;
        } else if (sample.length() == 3) {
            sample = "0" + sample;
        } else if (sample.length() == 4) {
            sample = sample;
        }
        return value + sample;
    }

    public String getSampleNO_for_register(String value) {

        Record re = Db.findFirst("select MAX(SAMPLENO) as SAMPLENO FROM lis_samplecollection WHERE date(RECEIVETIME)=CURDATE() and SAMPLENO LIKE '" + value + "%'");
        if (re == null || re.get("SAMPLENO") == null) {

            return value + "0001";

        }
        String sample = (Integer.parseInt(re.getStr("SAMPLENO").replace(value, "")) + 1) + "";
        System.out.println(sample);
        if (sample.length() == 1) {
            sample = "000" + sample;
        } else if ((re.getInt("id") + "").length() == 2) {
            sample = "00" + sample;
        } else if ((re.getInt("id") + "").length() == 3) {
            sample = "0" + sample;
        } else if ((re.getInt("id") + "").length() == 4) {
            sample = sample;
        }
        return value + sample;
    }

}
